import { getToken,getClientid } from "../utils/auth"
import {useProcessStore} from '@/store/index.js'

const processer = useProcessStore()
const baseUrl = ()=>{
	if(process.env.NODE_ENV === 'development'){
		return '/dev-api'
	}
	return 'http://192.168.8.130:9010'
}
export const uploadFile = (filePath,index,sendType)=>{
 return new Promise((resolve,reject)=>{
	const uploadTask = uni.uploadFile({
			url:`http://192.168.8.130:9010/business/deliveryDocument/uploadFile`,
			filePath,
			header: {
			 "Authorization": 'Bearer '+getToken(),
			 "clientid":getClientid()
		    },
			timeout:50000,
			name:"file",
			success:res=>{
			 const params = JSON.parse(res.data)
			 if(params.code === 200){
				 resolve(params.data)
			 }else{
				 resolve(null)
				 uni.showToast({
					 icon:"error",
					 title:params.msg
				 })
				 processer.deleteProcess(sendType,index,false)
			 }
			},
			fail(e) {
				reject("上传文件失败",e)
				 processer.deleteProcess(sendType,index,false)
			}
		})
		// 监听上传进度
		uploadTask.onProgressUpdate((val)=>{
			try{
				processer.changeProcess(val.progress,sendType,index)
			}catch(e){
				//TODO handle the exception
				// 文件上传出现问题直接中断上传
				uploadTask.abort()
			}
		})
		
	})

}